package com.slkj.shilixiaoyuanapp.ui.common;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.BodyartspecialtyEntity;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.tkk.api.RxEventProcessor;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SPR_FINSH;

@ActivityInfo(layout = R.layout.activity_chose_people, title = "选择对象", rightHint = "确定")
public class ChoseAuditorActivity extends BaseActivity {
    @BindView(R.id.recycer)
    RecyclerView mRecyclerView;
    BaseQuickAdapter adapter;
    @InjectParam
    ArrayList<BodyartspecialtyEntity.AuditorEntity> data;
    @InjectParam
    int code;
    @InjectParam
    String tag;
    @Override
    protected void init() {
        ChoseAuditorActivity__JumpCenter.bind(this);
        adapter = new BaseQuickAdapter<BodyartspecialtyEntity.AuditorEntity, BaseViewHolder>(R.layout.item_chose_people, data) {
            @Override
            protected void convert(BaseViewHolder helper, final BodyartspecialtyEntity.AuditorEntity item) {
                helper.setText(R.id.tv_name,item.getUName());
                ImageView imgCheck = helper.getView(R.id.img_head);
                ImageView imgUser = helper.getView(R.id.img_user);
                if (item.getHeadImg() != null){
                    Glide.with(ChoseAuditorActivity.this).load(item.getHeadImg()).asBitmap().centerCrop().into(imgUser);
                }else {
                    Glide.with(ChoseAuditorActivity.this)
                            .load(item.getHeadImg())
                            .error(R.drawable.ic_man)
                            .fallback(R.drawable.ic_man)
                            .into(imgUser);
                }
                imgCheck.setImageResource(item.isCheck() ? R.drawable.ic_checked :R.drawable.ic_un_checked);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item.setCheck(!item.isCheck());
                        imgCheck.setImageResource(item.isCheck() ? R.drawable.ic_checked :R.drawable.ic_un_checked);
                    }
                });
            }
        };
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    protected void onRightTitleClick() {
        List<Integer> uids = new ArrayList<>();
        List<BodyartspecialtyEntity.AuditorEntity> checkData = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {
            BodyartspecialtyEntity.AuditorEntity item = data.get(i);
            if (item.isCheck){
                checkData.add(item);
                uids.add(item.getUId());
            }
        }
        RxEventProcessor.get().post(tag+ CHOSE_SPR_FINSH, code,getPeoplesInfos(checkData),data,uids);
        finish();
    }

    /**
     * 将人员集合转换成id集合
     * @param data
     * @return
     */
    public static  List<Integer> getIdList( List<PeopleEntity> data){
        List<Integer> uids = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            PeopleEntity item = data.get(i);
            uids.add(item.getUId());
        }
        return uids;
    }


    public static String getPeoplesInfos( List<BodyartspecialtyEntity.AuditorEntity> checkData){
        String infos = "";
        if (checkData == null)
            return infos;
        switch (checkData.size()) {
            case 0:
                break;
            case 1:
                infos = checkData.get(0).getUName();
                break;
            case 2:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName();
                break;
            case 3:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName()+ "," + checkData.get(2).getUName();
                break;
            default:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName() + "," + checkData.get(2).getUName()+ "等" + checkData.size() + "人";
                break;
        }
        return  infos;
    }

}
